package com.bqtv.sport.ui.home.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.bqtv.sport.R
import com.bqtv.sport.adapter.LiveAnalyzeDataAdapter
import com.bqtv.sport.adapter.LiveAnalyzeHtftAdapter
import com.bqtv.sport.adapter.LiveAnalyzeMatchesAdapter
import com.bqtv.sport.adapter.LiveAnalyzePointAdapter
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.app.AppFragment
import com.bqtv.sport.databinding.FragmentLiveAnalyzeDetailBinding


class LiveAnalyzeDetailFragment :
    AppFragment<FragmentLiveAnalyzeDetailBinding, AppActivity<ViewBinding>>() {

    private lateinit var homePointAdapter: LiveAnalyzePointAdapter
    private lateinit var awayPointAdapter: LiveAnalyzePointAdapter
    private lateinit var historyDataAdapter: LiveAnalyzeDataAdapter
    private lateinit var recentHomeAdapter: LiveAnalyzeDataAdapter
    private lateinit var recentAwayAdapter: LiveAnalyzeDataAdapter
    private lateinit var homeMatchesAdapter: LiveAnalyzeMatchesAdapter
    private lateinit var awayMatchesAdapter: LiveAnalyzeMatchesAdapter
    private lateinit var htftAdapter: LiveAnalyzeHtftAdapter

    companion object {
        private const val INTENT_KEY_TYPE = "type"
        fun newInstance(): LiveAnalyzeDetailFragment {
            val fragment = LiveAnalyzeDetailFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }

        fun newInstance(type: String?): LiveAnalyzeDetailFragment {
            val fragment = LiveAnalyzeDetailFragment()
            val bundle = Bundle()
            bundle.putString(INTENT_KEY_TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getViewBinding() = FragmentLiveAnalyzeDetailBinding.inflate(layoutInflater)

    override fun initView() {
        binding.rcvPointHome.layoutManager = LinearLayoutManager(requireContext())
        binding.rcvPointAway.layoutManager = LinearLayoutManager(requireContext())
        homePointAdapter = LiveAnalyzePointAdapter()
        binding.rcvPointHome.adapter = homePointAdapter
        awayPointAdapter = LiveAnalyzePointAdapter()
        binding.rcvPointAway.adapter = awayPointAdapter

        binding.rcvHistory.layoutManager = LinearLayoutManager(requireContext())
        historyDataAdapter = LiveAnalyzeDataAdapter()
        binding.rcvHistory.adapter = historyDataAdapter

        binding.rcvRecentHome.layoutManager = LinearLayoutManager(requireContext())
        recentHomeAdapter = LiveAnalyzeDataAdapter()
        binding.rcvRecentHome.adapter = recentHomeAdapter

        binding.rcvRecentAway.layoutManager = LinearLayoutManager(requireContext())
        recentAwayAdapter = LiveAnalyzeDataAdapter()
        binding.rcvRecentAway.adapter = recentAwayAdapter

        binding.rcvMatchesHome.layoutManager = LinearLayoutManager(requireContext())
        homeMatchesAdapter = LiveAnalyzeMatchesAdapter()
        binding.rcvMatchesHome.adapter = homeMatchesAdapter
        binding.rcvMatchesAway.layoutManager = LinearLayoutManager(requireContext())
        awayMatchesAdapter = LiveAnalyzeMatchesAdapter()
        binding.rcvMatchesAway.adapter = awayMatchesAdapter

        binding.rcvHtft.layoutManager = LinearLayoutManager(requireContext())
        htftAdapter = LiveAnalyzeHtftAdapter()
        binding.rcvHtft.adapter = htftAdapter
    }

    override fun initData() {

        initListener()
    }

    private fun initListener() {
        setOnClickListener(binding.tvHistoryFive, binding.tvHistoryTen, binding.tvHistoryTwenty,
            binding.tvTabGoal, binding.tvTabCurrentPoints, binding.tvTabClash, binding.tvTabGame,
            binding.tvTabHtft)
    }

    override fun onClick(view: View) {
        super.onClick(view)
        when (view) {
            binding.tvHistoryFive -> {
                binding.tvHistoryFive.setBackgroundColor(getResources().getColor(R.color.transparent))
                binding.tvHistoryTen.setBackgroundColor(getResources().getColor(R.color.transparent))
                binding.tvHistoryTwenty.setBackgroundColor(getResources().getColor(R.color.transparent))

                binding.tvHistoryFive.setBackgroundResource(R.drawable.bg_round_ffffff)
            }

            binding.tvHistoryTen -> {
                binding.tvHistoryFive.setBackgroundColor(getResources().getColor(R.color.transparent))
                binding.tvHistoryTen.setBackgroundColor(getResources().getColor(R.color.transparent))
                binding.tvHistoryTwenty.setBackgroundColor(getResources().getColor(R.color.transparent))
                binding.tvHistoryTen.setBackgroundResource(R.drawable.bg_round_ffffff)
            }

            binding.tvHistoryTwenty -> {
                binding.tvHistoryFive.setBackgroundColor(getResources().getColor(R.color.transparent))
                binding.tvHistoryTen.setBackgroundColor(getResources().getColor(R.color.transparent))
                binding.tvHistoryTwenty.setBackgroundColor(getResources().getColor(R.color.transparent))
                binding.tvHistoryTwenty.setBackgroundResource(R.drawable.bg_round_ffffff)
            }
            binding.tvTabGoal -> {
                setScrollType(0)
                val targetY: Int = binding.llGoal.getTop() // 获取目标View的顶部位置
                binding.scrollView.smoothScrollTo(0, targetY)
            }
            binding.tvTabCurrentPoints -> {
                setScrollType(1)
                val targetY: Int = binding.llCurrentPoints.getTop()
                binding.scrollView.smoothScrollTo(0, targetY)
            }
            binding.tvTabClash -> {
                setScrollType(2)
                val targetY: Int = binding.llClash.getTop()
                binding.scrollView.smoothScrollTo(0, targetY)
            }
            binding.tvTabGame -> {
                setScrollType(3)
                val targetY: Int = binding.llGame.getTop()
                binding.scrollView.smoothScrollTo(0, targetY)
            }
            binding.tvTabHtft -> {
                setScrollType(4)
                val targetY: Int = binding.llHtft.getTop()
                binding.scrollView.smoothScrollTo(0, targetY)
            }
        }
    }

    private fun setScrollType(type: Int) {
        binding.tvTabGoal.setTextColor(Color.parseColor("#2F6146"))
        binding.tvTabGoal.setBackgroundColor(Color.parseColor("#FFFFFF"))
        binding.tvTabCurrentPoints.setTextColor(Color.parseColor("#2F6146"))
        binding.tvTabCurrentPoints.setBackgroundColor(Color.parseColor("#FFFFFF"))
        binding.tvTabClash.setTextColor(Color.parseColor("#2F6146"))
        binding.tvTabClash.setBackgroundColor(Color.parseColor("#FFFFFF"))
        binding.tvTabGame.setTextColor(Color.parseColor("#2F6146"))
        binding.tvTabGame.setBackgroundColor(Color.parseColor("#FFFFFF"))
        binding.tvTabHtft.setTextColor(Color.parseColor("#2F6146"))
        binding.tvTabHtft.setBackgroundColor(Color.parseColor("#FFFFFF"))
        when (type) {
            0 -> {
                binding.tvTabGoal.setTextColor(Color.parseColor("#FFFFFF"))
                binding.tvTabGoal.setBackgroundResource(R.drawable.button_theme)
            }

            1 -> {
                binding.tvTabCurrentPoints.setTextColor(Color.parseColor("#FFFFFF"))
                binding.tvTabCurrentPoints.setBackgroundResource(R.drawable.button_theme)
            }

            2 -> {
                binding.tvTabClash.setTextColor(Color.parseColor("#FFFFFF"))
                binding.tvTabClash.setBackgroundResource(R.drawable.button_theme)
            }

            3 -> {
                binding.tvTabGame.setTextColor(Color.parseColor("#FFFFFF"))
                binding.tvTabGame.setBackgroundResource(R.drawable.button_theme)
            }
            4 -> {
                binding.tvTabHtft.setTextColor(Color.parseColor("#FFFFFF"))
                binding.tvTabHtft.setBackgroundResource(R.drawable.button_theme)
            }
        }
    }
}

